package net.example.lesson3;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest1 {
    public static void main(String[] args) {
        final Hello hello = new HelloImpl();
        hello.sayHello("hello。。。。。。。。。。。。");
        final MyInvocationHandler myInvocationHandler = new MyInvocationHandler(new HelloImpl());

        final Hello helloProxy = (Hello)Proxy.newProxyInstance(HelloImpl.class.getClassLoader(),
                HelloImpl.class.getInterfaces(), myInvocationHandler);
        helloProxy.sayHello("哈哈。。。。。。。。。。。。");
    }
}

interface Hello{
    /**
     * sayHello
     * @param hello
     */
    void sayHello(String hello);
}

class HelloImpl implements Hello{
    @Override
    public void sayHello(String hello){
        System.out.println(hello);
    }
}

class MyInvocationHandler implements InvocationHandler{
    private Object target;

    public MyInvocationHandler(Object target){
        this.target = target;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable{
        System.out.println("Invoking sayHello");
        Object ressult = method.invoke(target, args);
        return ressult;
    }
}